package cn.zhiquxing;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

@Slf4j
public class IntegrityDemo {
    
    public String rpc1() throws InterruptedException {
        TimeUnit.MILLISECONDS.sleep(600);
        log.info("rpc1");
        return "sth1.  rpc1";
    }
    
    public String rpc2() throws InterruptedException {
        TimeUnit.MILLISECONDS.sleep(600);
        log.info("rpc2");
        return "sth2.  rpc2";
    }
    
    @Test
    public void test() throws InterruptedException, ExecutionException, TimeoutException {
        CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
            return "sth1.";
        });
        
        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
            return "sth2.";
        });

        CompletableFuture<String> future = future1.thenCombine(future2, (f1, f2) -> {
            return f1 + f2;
        });

        String s = future.get(2, TimeUnit.SECONDS);
        log.info("result => {}", s);
    }
    
}
